"""
配置预设数据模型
用于管理应用、OBS桶配置、资源池配置、镜像配置等预设
"""
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, ForeignKey
from sqlalchemy.orm import relationship
from database import Base


class Application(Base):
    """应用配置表"""
    __tablename__ = "applications"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, unique=True, index=True, comment="应用名称")
    description = Column(Text, nullable=True, comment="描述")
    appid = Column(String(255), nullable=True, comment="应用ID")
    hw_id = Column(String(255), nullable=True, comment="凭证ID")
    hw_appkey = Column(String(255), nullable=True, comment="认证密钥")
    vender = Column(String(100), nullable=True, comment="供应商")
    region = Column(String(100), nullable=True, comment="区域")
    trainApiVersion = Column(String(100), nullable=True, comment="训练作业API版本")
    is_active = Column(Boolean, nullable=False, default=True, comment="是否启用")
    created_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")), onupdate=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    created_by = Column(String(100), nullable=True)

    # 关系
    obs_bucket_presets = relationship("ObsBucketPreset", back_populates="application")
    resource_pool_presets = relationship("ResourcePoolPreset", back_populates="application")
    image_presets = relationship("ImagePreset", back_populates="application")
    nas_presets = relationship("NasPreset", back_populates="application")

    def __repr__(self):
        return f"<Application(id={self.id}, name='{self.name}')>"


class ObsBucketPreset(Base):
    """OBS桶配置预设表"""
    __tablename__ = "obs_bucket_presets"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, unique=True, index=True, comment="预设名称（中文名称）")
    bucket_id = Column(String(255), nullable=False, comment="桶ID")
    bucket_path = Column(String(500), nullable=True, comment="桶路径")
    description = Column(Text, nullable=True, comment="描述")
    is_active = Column(Boolean, nullable=False, default=True, comment="是否启用")
    application_id = Column(Integer, ForeignKey("applications.id"), nullable=False, index=True, comment="所属应用ID")
    created_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")), onupdate=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    created_by = Column(String(100), nullable=True)

    # 关系
    application = relationship("Application", back_populates="obs_bucket_presets")

    def __repr__(self):
        return f"<ObsBucketPreset(id={self.id}, name='{self.name}', bucket_id='{self.bucket_id}')>"


class ResourcePoolPreset(Base):
    """资源池配置预设表"""
    __tablename__ = "resource_pool_presets"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, unique=True, index=True, comment="预设名称（中文名称）")
    pool_type = Column(String(100), nullable=False, comment="资源池类型")
    pool_uid = Column(String(255), nullable=False, comment="资源池UID")
    spec_name = Column(String(255), nullable=False, comment="资源池规格名称")
    flavor_code = Column(String(255), nullable=False, comment="资源池编码")
    description = Column(Text, nullable=True, comment="描述")
    is_active = Column(Boolean, nullable=False, default=True, comment="是否启用")
    application_id = Column(Integer, ForeignKey("applications.id"), nullable=False, index=True, comment="所属应用ID")
    created_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")), onupdate=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    created_by = Column(String(100), nullable=True)

    # 关系
    application = relationship("Application", back_populates="resource_pool_presets")

    def __repr__(self):
        return f"<ResourcePoolPreset(id={self.id}, name='{self.name}', pool_uid='{self.pool_uid}')>"


class ImagePreset(Base):
    """镜像配置预设表"""
    __tablename__ = "image_presets"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, unique=True, index=True, comment="预设名称（中文名称）")
    image_version = Column(String(255), nullable=True, comment="镜像版本")
    image_uid = Column(String(255), nullable=False, comment="镜像UID")
    image_version_uid = Column(String(255), nullable=False, comment="镜像版本UID")
    description = Column(Text, nullable=True, comment="描述")
    is_active = Column(Boolean, nullable=False, default=True, comment="是否启用")
    application_id = Column(Integer, ForeignKey("applications.id"), nullable=False, index=True, comment="所属应用ID")
    created_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")), onupdate=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    created_by = Column(String(100), nullable=True)

    # 关系
    application = relationship("Application", back_populates="image_presets")

    def __repr__(self):
        return f"<ImagePreset(id={self.id}, name='{self.name}', image_uid='{self.image_uid}')>"


class NasPreset(Base):
    """NAS配置预设表"""
    __tablename__ = "nas_presets"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(255), nullable=False, unique=True, index=True, comment="预设名称（中文名称）")
    nas_name = Column(String(255), nullable=False, comment="NAS名称")
    nas_type = Column(String(100), nullable=False, comment="NAS类型，如efs")
    nas_uid = Column(String(255), nullable=False, comment="NAS UID")
    nas_instance_id = Column(String(255), nullable=False, comment="NAS实例ID")
    nas_shar_addr = Column(String(500), nullable=False, comment="NAS路径")
    description = Column(Text, nullable=True, comment="描述")
    is_active = Column(Boolean, nullable=False, default=True, comment="是否启用")
    application_id = Column(Integer, ForeignKey("applications.id"), nullable=False, index=True, comment="所属应用ID")
    created_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    updated_at = Column(DateTime(timezone=True), nullable=False, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai")), onupdate=lambda: datetime.now(ZoneInfo("Asia/Shanghai")))
    created_by = Column(String(100), nullable=True)

    # 关系
    application = relationship("Application", back_populates="nas_presets")

    def __repr__(self):
        return f"<NasPreset(id={self.id}, name='{self.name}', nas_name='{self.nas_name}')>"
